package com.yy.personalcv.controller;

import com.yy.personalcv.pojo.Internship;
import com.yy.personalcv.pojo.UserInformation;
import com.yy.personalcv.responce.InternshipResponce;
import com.yy.personalcv.service.InternshipService;
import com.yy.personalcv.tool.StringUtils;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@Slf4j
public class InternshipController {
    @Resource
    private InternshipService internshipService;

    @RequestMapping(value = "/internshipList.do")
    public String getInternshipList(HttpServletRequest request, Model model,
                                 @RequestParam(defaultValue = "1") int page,
                                 @RequestParam(defaultValue = "5") int size){
        UserInformation userInformation = (UserInformation) request.getSession().getAttribute("userInformation");
        if(userInformation == null){
            return "redirect:/login.do";
        }
        int uid = userInformation.getId();
        int counts = getInternshipCounts(uid);
        List<Internship> internships = selectTen(page, size, uid);
        model.addAttribute("internships", internships);
        model.addAttribute("page", page);
        model.addAttribute("size", internships.size());
        model.addAttribute("counts", counts);
        model.addAttribute("max_size", size);
        model.addAttribute("userInformation", userInformation);
        return "internship_page";
    }

    @RequestMapping(value = "/internshipDetail.do")
    public String getInternshipDetail(HttpServletRequest request, Model model,
                                 @RequestParam int id){
        UserInformation userInformation = (UserInformation) request.getSession().getAttribute("userInformation");
        if(userInformation == null){
            return "redirect:/login.do";
        }
        int uid = userInformation.getId();
        Internship internship = getInternshipDetail(id, uid);
        if(internship == null){
            return "redirect:/internshipList";
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        InternshipResponce internshipResponce = new InternshipResponce();
        internshipResponce.setId(internship.getId());
        internshipResponce.setCompany(internship.getCompany());
        internshipResponce.setStartTime(sdf.format(internship.getStartTime()));
        internshipResponce.setEndTime(sdf.format(internship.getEndTime()));
        internshipResponce.setDescription(internship.getDescription());
        internshipResponce.setModified(sdf.format(internship.getModified()));
        internshipResponce.setDisplay(internship.getDisplay());
        internshipResponce.setUid(internship.getUid());
        model.addAttribute("internship", internshipResponce);
        model.addAttribute("userInformation", userInformation);
        return "internship_detail_page";
    }

    @RequestMapping(value = "/addInternship.do")
    public String addInternship(HttpServletRequest request, Model model,
                               @RequestParam String company, @RequestParam String start, @RequestParam String end, @RequestParam String description) throws ParseException {
        UserInformation userInformation = (UserInformation) request.getSession().getAttribute("userInformation");
        if(userInformation == null){
            return "redirect:/login.do";
        }
        if(StringUtils.getInstance().isNullOrEmpty(company)
                || StringUtils.getInstance().isNullOrEmpty(start)
                || StringUtils.getInstance().isNullOrEmpty(end)
                || StringUtils.getInstance().isNullOrEmpty(description)){
            log.error("one of them is null");
            return "redirect:/internshipList.do";
        }
        int uid = userInformation.getId();
        Internship internship = new Internship();
        internship.setCompany(company);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date startDate = sdf.parse(start);
        Date endDate = sdf.parse(end);
        internship.setStartTime(startDate);
        internship.setEndTime(endDate);
        internship.setDescription(description);
        internship.setUid(uid);
        internshipService.addInternship(internship);
        model.addAttribute("userInformation", userInformation);
        return "redirect:/internshipList.do";
    }

    @RequestMapping(value = "/internshipDelete.do")
    public String deleteSkill(HttpServletRequest request, Model model,
                              @RequestParam int id){
        UserInformation userInformation = (UserInformation) request.getSession().getAttribute("userInformation");
        if(userInformation == null){
            return "redirect:/login.do";
        }
        if(StringUtils.getInstance().isNullOrEmpty(id)){
            log.error("id is null");
            return "redirect:/internshipList.do";
        }
        int uid = userInformation.getId();
        internshipService.deleteInternship(id, uid);
        model.addAttribute("userInformation", userInformation);
        return "redirect:/internshipList.do";
    }

    private List<Internship> selectTen(int page, int size, int uid){
        Map map = new HashMap<>();
        map.put("start", (page-1)*size);
        map.put("size", size);
        map.put("uid", uid);
        return internshipService.selectTen(map);
    }

    @RequestMapping(value = "/internshipUpdate.do", method = RequestMethod.POST)
    public String updateInternship(HttpServletRequest request, Model model,
                                  @RequestParam int id, @RequestParam String company, @RequestParam String start, @RequestParam String end, @RequestParam String description) throws ParseException {
        UserInformation userInformation = (UserInformation) request.getSession().getAttribute("userInformation");
        if(userInformation == null){
            return "redirect:/login.do";
        }
        if(StringUtils.getInstance().isNullOrEmpty(company)
                || StringUtils.getInstance().isNullOrEmpty(start)
                || StringUtils.getInstance().isNullOrEmpty(end)
                || StringUtils.getInstance().isNullOrEmpty(description)){
            log.error("one of them is null");
            return "redirect:/internshipList.do";
        }
        int uid = userInformation.getId();
        Internship internship = new Internship();
        internship.setId(id);
        internship.setCompany(company);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date startDate = sdf.parse(start);
        Date endDate = sdf.parse(end);
        internship.setStartTime(startDate);
        internship.setEndTime(endDate);
        internship.setDescription(description);
        internship.setUid(uid);
        internshipService.updateInternship(internship);
        Internship internship1 = getInternshipDetail(id, uid);
        InternshipResponce internshipResponce = new InternshipResponce();
        internshipResponce.setId(internship1.getId());
        internshipResponce.setCompany(internship1.getCompany());
        internshipResponce.setStartTime(sdf.format(internship1.getStartTime()));
        internshipResponce.setEndTime(sdf.format(internship1.getEndTime()));
        internshipResponce.setDescription(internship1.getDescription());
        internshipResponce.setModified(sdf.format(internship1.getModified()));
        internshipResponce.setDisplay(internship1.getDisplay());
        internshipResponce.setUid(internship1.getUid());
        model.addAttribute("internship", internshipResponce);
        model.addAttribute("userInformation", userInformation);
        return "internship_detail_page";
    }

    private int getInternshipCounts(int uid){
        return internshipService.getInternshipCounts(uid);
    }

    private Internship getInternshipDetail(int id, int uid){
        return internshipService.getInternshipDetail(id, uid);
    }


}
